import os
import subprocess
import config

# noinspection PyPackageRequirements
import wx

from .helpers import AutoListCtrl

_t = wx.GetTranslation

class ItemEffects(wx.Panel):
    def __init__(self, parent, stuff, item):
        wx.Panel.__init__(self, parent)
        self.item = item

        mainSizer = wx.BoxSizer(wx.VERTICAL)

        self.effectList = AutoListCtrl(self, wx.ID_ANY,
                                       style=wx.LC_REPORT | wx.LC_SINGLE_SEL | wx.LC_VRULES | wx.NO_BORDER)
        mainSizer.Add(self.effectList, 1, wx.ALL | wx.EXPAND, 0)
        self.SetSizer(mainSizer)

        self.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.OnClick, self.effectList)

        self.PopulateList()

    def PopulateList(self):

        self.effectList.InsertColumn(0, _t("Name"))
        self.effectList.InsertColumn(1, _t("Active"))
        self.effectList.InsertColumn(2, _t("Type"))
        if config.debug:
            self.effectList.InsertColumn(3, _t("Run Time"))
            self.effectList.InsertColumn(4, _t("ID"))

        # self.effectList.SetColumnWidth(0,385)

        self.effectList.setResizeColumn(0)
        self.effectList.SetColumnWidth(1, 50)
        self.effectList.SetColumnWidth(2, 80)
        if config.debug:
            self.effectList.SetColumnWidth(3, 65)
            self.effectList.SetColumnWidth(4, 40)

        item = self.item
        self.effects = effects = item.effects
        names = list(effects.keys())
        names.sort()

        for name in names:
            dict={"loPower": "低能量",
                    "hiPower": "高能量",
                    "medPower": "中级能量",
                    "launcherFitted": "发射器",
                    "turretFitted": "炮台",
                    "turretWeaponCapacitorNeedMultiply": "炮台武器电容修正值",
                    "damageMultiplierSkillBonus": "伤害加成",
                    "skillBoostDamageMultiplierBonus": "技能增加伤害修正值加成",
                    "barrage": "弹幕攻击",
                    "shipHTDmgBonusGB": "舰船混合炮台伤害加成",
                    "shipHTDmgBonusfixedGC": "舰船炮台伤害加成固定GC",
                    "warpScrambleForEntity": "跃迁干扰",
                    "missileLaunchingForEntity": "导弹发射",
                    "modifyTargetSpeed2": "修正目标速度",
                    "decreaseTargetSpeed": "降低目标速度",
                    "cloaking": "隐身",
                    "shipSpecialityMining": "深核采矿CPU减少",
                    "anchorDrop": "锚定",
                    "anchorLift": "取消锚定",
                    "agilityMultiplierEffect": "敏捷乘数效果",
                    "antiWarpScramblingPassive": "反跃迁干扰",
                    "shipVelocityBonusAI": "舰船速度加成AI",
                    "onlineForStructures": "启用",
                    "cloakingWarpSafe": "隐身",
                    "anchorDropForStructures": "锚定",
                    "anchorLiftForStructures": "取消锚定",
                    "projectileFiredForEntities": "射弹已发射",
                    "structureUnanchorForced": "强行解除锚定",
                    "targetAttackForStructures": "目标攻击",
                    "miningInfoMultiplier": "采矿信息乘数",
                    "shipEnergyVampireTransferAmountBonusAc": "舰船能量吸食转移量加成AB",
                    "leadershipCpuBonus": "后勤CPU减少",
                    "flagshipmultiRelayEffect": "多重中继效果",
                    "armoredCommandMindlink": "装甲指挥思维网络芯片",
                    "skirmishCommandMindlink": "游击战指挥思维网络芯片",
                    "shieldCommandMindlink": "护盾指挥思维网络芯片",
                    "informationCommandMindlink": "护盾指挥思维网络芯片",
                    "selfRof": "自身发射速率",
                    "ammoEMResistOther": "EM抗性乘数",
                    "implantVelocityBonus": "最大速度加成",
                    "warpScrambleForStructure": "跃迁干扰",
                    "rigSlot": "改装件插槽",
                    "boosterArmorHpPenalty": "装甲强度值惩罚",
                    "boosterArmorRepairAmountPenalty": "装甲维修器惩罚",
                    "boosterShieldCapacityPenalty": "护盾容量值惩罚",
                    "boosterTurretOptimalRangePenalty": "炮台最佳射程惩罚",
                    "boosterTurretFalloffPenalty": "炮台失准范围惩罚",
                    "boosterCapacitorCapacityPenalty": "电容容量惩罚",
                    "boosterMaxVelocityPenalty": "最大速度惩罚",
                    "boosterTurretTrackingPenalty": "炮台跟踪惩罚",
                    "boosterMissileVelocityPenalty": "导弹飞行速度惩罚",
                    "boosterMissileExplosionVelocityPenalty": "爆炸速度惩罚",
                    "boosterMissileExplosionCloudPenaltyFixed": "导弹爆炸半径惩罚",
                    "velocityBonusOnline": "最大速度加成",
                    "concordWarpScramble": "跃迁干扰",
                    "concordModifyTargetSpeed": "修正目标速度",
                    "agilityMultiplierEffectPassive": "敏捷乘数效果被动性",
                    "velocityBonusPassive": "最大速度加成",
                    "subSystem": "子系统",
                    "anchorDropOrbital": "正在锚定",
                    "anchorLiftOrbital": "正在解锚",
                    "onlineOrbital": "在线",
                    "boosterShieldBoostAmountPenaltyShieldSkills": "护盾回充增效器惩罚",
                    "warpScrambleTargetMWDBlockActivationForEntity": "跃迁干扰",
                    "serviceSlot": "服务槽位",
                    "rigMoonSpewVolBonus": "钻探体积加成",
                    "rigMoonSpewRadBonus": "卫星小行星带半径加成",
                    "rigMoonFractureDelay": "小行星开采延时加成",
                    "rigMoonAsteroidDecayBonus": "卫星小行星衰减加成",
                  "online": "在线",
                  "useMissiles": "使用导弹",
                  "overloadRofBonus": "超载射击速度加成"
                 }
            index = self.effectList.InsertItem(self.effectList.GetItemCount(), dict.get(name) if(dict.get(name)) else name)

            if effects[name].isImplemented:
                if effects[name].activeByDefault:
                    activeByDefault = _t("Yes")
                else:
                    activeByDefault = _t("No")
            else:
                activeByDefault = ""

            effectTypeText = ""
            if effects[name].type:
                for effectType in effects[name].type:
                    dict={"passive":"被动","offline":"离线","gang":"编组","active":"激活","projected":"投放","":"无","overheat":"超载"}
                    effectTypeText += dict.get(effectType) if(dict.get(effectType)) else effectType + " "
                    pass

            if effects[name].runTime and effects[name].isImplemented:
                effectRunTime = str(effects[name].runTime)
            else:
                effectRunTime = ""

            self.effectList.SetItem(index, 1, activeByDefault)
            self.effectList.SetItem(index, 2, effectTypeText)
            if config.debug:
                self.effectList.SetItem(index, 3, effectRunTime)
                self.effectList.SetItem(index, 4, str(effects[name].ID))

        self.effectList.RefreshRows()
        self.Layout()

    def OnClick(self, event):
        """
        Debug use: toggle effects on/off.
        Affects *ALL* items that use that effect.
        Is not stateful.  Will reset if Pyfa is closed and reopened.
        """

        try:
            activeByDefault = getattr(self.item.effects[event.GetText()], "activeByDefault")
            if activeByDefault:
                setattr(self.item.effects[event.GetText()], "activeByDefault", False)
            else:
                setattr(self.item.effects[event.GetText()], "activeByDefault", True)

        except AttributeError:
            # Attribute doesn't exist, do nothing
            pass

        self.RefreshValues(event)

    def RefreshValues(self, event):
        self.Freeze()
        self.effectList.ClearAll()
        self.PopulateList()
        self.effectList.RefreshRows()
        self.Layout()
        self.Thaw()
        event.Skip()
